<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <script src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vue/2.6.14/vue.js" type="application/javascript"></script>
</head>
<body>
    <div id="app">
      <el-input ref="inputRef" v-if="isShow"></el-input>
      <button @click="onClick">按钮</button>
    </div>
</body>
<script >
    Vue.component('el-input', {
      template: `
        <input ref="inputEleRef"/>
      `,
      methods: {
        focus(){
          this.$refs.inputEleRef.focus()
        }
      },
    })
    new Vue({
        el: '#app',
        data() {
            return {
              isShow: false
            }
        },
        methods: {
          async onClick(){
            // setTimeout(() => {
            //   this.$refs.inputRef.focus()
            // })
            //修改isShow为true,但是页面输入没有立即显示,因为vue渲染模板是异步的(微任务Promise.then)
            //异步代码
            //宏任务: setTimeout
            //微任务: Promise.then
            //执行规律
            //(1)宏任务先执行,在执行所有立即需要执行微任务
            //(2)同级条件,先执行微任务,然后执行宏任务
            this.isShow = true
            //产生一个微任务
            /*
            let p = new Promise((resovle,reject)=>{
              resovle()
            })
            p.then(()=>{
              this.$refs.inputRef.focus()
            })
            */
            //通过语法糖
            /*
            Promise.resolve().then(()=>{
              this.$refs.inputRef.focus()
            })
            */
            // await Promise.resolve()
            // this.$refs.inputRef.focus()
            // $nextTick
            /*
            this.$nextTick(()=>{
              this.$refs.inputRef.focus()
            })
            */
            // await this.$nextTick()
            // this.$refs.inputRef.focus()
            //产生一个宏任务
            setTimeout(() => {
              this.$refs.inputRef.focus()
            })
          }
        }
    })
</script>
</html>
